﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    public partial class MainService : IPremium
    {
        public DataSet Premium_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Premium_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public DataSet Premium_GetAllByEmployeeID(int EmployeeID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Premium_GetAllByEmployeeID", dbConn);
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Premium_QTBH(string Branch_Code, string SIBookNo, int opt,DateTime FromDate, DateTime ToDate, int DocumentID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd;
            dbCmd = new SqlCommand("HRM_Premium_QTBH", dbConn);
            dbCmd.Parameters.AddWithValue("@Branch_Code", Branch_Code);
            dbCmd.Parameters.AddWithValue("@Detail_InsBookNo", SIBookNo);
            dbCmd.Parameters.AddWithValue("@opt", opt);
            dbCmd.Parameters.AddWithValue("@Fromdate", FromDate != DateTime.MinValue ? FromDate : System.Data.SqlTypes.SqlDateTime.Null);
            dbCmd.Parameters.AddWithValue("@Todate", ToDate != DateTime.MaxValue ? ToDate : System.Data.SqlTypes.SqlDateTime.Null);
            dbCmd.Parameters.AddWithValue("@DocumentID",DocumentID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Premium_GetBySIBookNo(string _SIBookNo, bool _IsQTBH)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd;
            if (_IsQTBH)
                dbCmd = new SqlCommand("HRM_Premium_QTBH", dbConn);
            else
                dbCmd = new SqlCommand("HRM_Premium_GetInfoBySIBookNo", dbConn);
            dbCmd.Parameters.AddWithValue("@SIBookNo", _SIBookNo);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Premium_GetByDate(DateTime _FromDate, DateTime _ToDate, string _BranchCode, int _InToKhai, string _SIBookNo)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Premium_GetByDate", dbConn);
            dbCmd.Parameters.AddWithValue("@FromDate", _FromDate);
            dbCmd.Parameters.AddWithValue("@ToDate", _ToDate);
            dbCmd.Parameters.AddWithValue("@BranchCode", _BranchCode);
            dbCmd.Parameters.AddWithValue("@InToKhai", _InToKhai);
            dbCmd.Parameters.AddWithValue("@SIBookNo", _SIBookNo);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public DataSet Premium_GetAmount(DateTime _FromDate, string _SIBookNo)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Premium_GetAmount", dbConn);
            dbCmd.Parameters.AddWithValue("@FromDate", _FromDate);
            dbCmd.Parameters.AddWithValue("@SIBookNo", _SIBookNo);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public decimal Premium_GetAVGAmount(DateTime _DateVal, string _SIBookNo)
        {
            decimal retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Premium_GetAVGAmount", dbConn);
            dbCmd.Parameters.AddWithValue("@DateVal", _DateVal);
            dbCmd.Parameters.AddWithValue("@SIBookNo", _SIBookNo);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                retVal = Convert.ToDecimal(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public int Premium_CountMonth(string _SIBookNo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Premium_CountMonth", dbConn);
            dbCmd.Parameters.AddWithValue("@SIBookNo", _SIBookNo);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public DataSet Premium_GetList(out int ItemCount, int EmployeeID, int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Premium_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public PremiumInfo Premium_GetInfo(int _Premium_ID)
        {
            PremiumInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Premium_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Premium_ID", _Premium_ID);
            SqlDataReader dr = null;

            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new PremiumInfo();
                    retVal.ID = Convert.ToInt32(dr["Premium_ID"]);
                    retVal.EmployeeID = Convert.ToInt32(dr["Premium_EmployeeID"]);
                    retVal.FromDate = dr["Premium_FromDate"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["Premium_FromDate"]);
                    retVal.ToDate = dr["Premium_ToDate"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["Premium_ToDate"]);
                    retVal.SIBookNo = Convert.ToString(dr["Premium_SIBookNo"]);
                    retVal.WorkUnit = Convert.ToString(dr["Premium_WorkUnit"]);
                    retVal.WorkName = Convert.ToString(dr["Premium_WorkName"]);
                    retVal.Job = Convert.ToString(dr["Premium_Job"]);
                    retVal.Type = Convert.ToString(dr["Premium_Type"]);
                    retVal.Salary = Convert.ToDecimal(dr["Premium_Salary"]);
                    retVal.BasicSalary = Convert.ToDecimal(dr["Premium_BasicSalary"]);
                    retVal.BasicFactor = Convert.ToDecimal(dr["Premium_BasicFactor"]);
                    retVal.PositionFactor = Convert.ToDecimal(dr["Premium_PositionFactor"]);
                    retVal.SeniorityFactor = Convert.ToDecimal(dr["Premium_SeniorityFactor"]);
                    retVal.OverSeniorityFactor = Convert.ToDecimal(dr["Premium_OverSeniorityFactor"]);
                    retVal.AreaFactor = Convert.ToDecimal(dr["Premium_AreaFactor"]);
                    retVal.MonthNo = Convert.ToInt32(dr["Premium_MonthNo"]);
                    retVal.InsTypeCode = Convert.ToString(dr["Premium_InsTypeCode"]);
                    retVal.Description = Convert.ToString(dr["Premium_Description"]);
                    retVal.DateVal = dr["Premium_Date"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["Premium_Date"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int Premium_Insert(PremiumInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Premium_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Premium_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@Premium_SIBookNo", eInfo.SIBookNo);
            dbCmd.Parameters.AddWithValue("@Premium_FromDate", eInfo.FromDate);
            dbCmd.Parameters.AddWithValue("@Premium_ToDate", eInfo.ToDate);
            dbCmd.Parameters.AddWithValue("@Premium_WorkUnit", eInfo.WorkUnit);
            dbCmd.Parameters.AddWithValue("@Premium_WorkName", eInfo.WorkName);
            dbCmd.Parameters.AddWithValue("@Premium_Job", eInfo.Job);
            dbCmd.Parameters.AddWithValue("@Premium_Type", eInfo.Type);
            dbCmd.Parameters.AddWithValue("@Premium_Salary", eInfo.Salary);
            dbCmd.Parameters.AddWithValue("@Premium_BasicSalary", eInfo.BasicSalary);
            dbCmd.Parameters.AddWithValue("@Premium_BasicFactor", eInfo.BasicFactor);
            dbCmd.Parameters.AddWithValue("@Premium_PositionFactor", eInfo.PositionFactor);
            dbCmd.Parameters.AddWithValue("@Premium_SeniorityFactor", eInfo.SeniorityFactor);
            dbCmd.Parameters.AddWithValue("@Premium_OverSeniorityFactor", eInfo.OverSeniorityFactor);
            dbCmd.Parameters.AddWithValue("@Premium_AreaFactor", eInfo.AreaFactor);
            dbCmd.Parameters.AddWithValue("@Premium_MonthNo", eInfo.MonthNo);
            dbCmd.Parameters.AddWithValue("@Premium_InsTypeCode", eInfo.InsTypeCode);
            dbCmd.Parameters.AddWithValue("@Premium_Description", eInfo.Description);
            dbCmd.Parameters.AddWithValue("@Premium_IsInitial", eInfo.IsInitial);
            dbCmd.Parameters.AddWithValue("@Premium_Date", eInfo.DateVal);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool Premium_Update(PremiumInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Premium_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Premium_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@Premium_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@Premium_SIBookNo", eInfo.SIBookNo);
            dbCmd.Parameters.AddWithValue("@Premium_FromDate", eInfo.FromDate);
            dbCmd.Parameters.AddWithValue("@Premium_ToDate", eInfo.ToDate);
            dbCmd.Parameters.AddWithValue("@Premium_WorkUnit", eInfo.WorkUnit);
            dbCmd.Parameters.AddWithValue("@Premium_WorkName", eInfo.WorkName);
            dbCmd.Parameters.AddWithValue("@Premium_Job", eInfo.Job);
            dbCmd.Parameters.AddWithValue("@Premium_Type", eInfo.Type);
            dbCmd.Parameters.AddWithValue("@Premium_Salary", eInfo.Salary);
            dbCmd.Parameters.AddWithValue("@Premium_BasicSalary", eInfo.BasicSalary);
            dbCmd.Parameters.AddWithValue("@Premium_BasicFactor", eInfo.BasicFactor);
            dbCmd.Parameters.AddWithValue("@Premium_PositionFactor", eInfo.PositionFactor);
            dbCmd.Parameters.AddWithValue("@Premium_SeniorityFactor", eInfo.SeniorityFactor);
            dbCmd.Parameters.AddWithValue("@Premium_OverSeniorityFactor", eInfo.OverSeniorityFactor);
            dbCmd.Parameters.AddWithValue("@Premium_AreaFactor", eInfo.AreaFactor);
            dbCmd.Parameters.AddWithValue("@Premium_MonthNo", eInfo.MonthNo);
            dbCmd.Parameters.AddWithValue("@Premium_InsTypeCode", eInfo.InsTypeCode);
            dbCmd.Parameters.AddWithValue("@Premium_Description", eInfo.Description);
            dbCmd.Parameters.AddWithValue("@Premium_Date", eInfo.DateVal);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool Premium_Delete(int PremiumID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Premium_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Premium_ID", PremiumID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool Premium_Exist(int _ID, string _SIBookNo, DateTime _FromDate, DateTime _ToDate)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Premium_Exist", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Premium_ID", _ID);
            dbCmd.Parameters.AddWithValue("@Premium_SIBookNo", _SIBookNo);
            dbCmd.Parameters.AddWithValue("@FromDate", _FromDate);
            dbCmd.Parameters.AddWithValue("@ToDate", _ToDate);
            try
            {
                dbConn.Open();
                if (Convert.ToInt32(dbCmd.ExecuteScalar()) > 0)
                    retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}
